package org.neframework.webtest.action.code;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import org.neframework.webtest.base.BaseController;
import org.neframework.webtest.model.t_s_code;
import org.neframework.webtest.service.CodeService;
import org.neframework.webtest.util.date.DateTools;

import com.tmsps.neframework.core.utils.GenerateTools;
import com.tmsps.neframework.core.utils.JsonTools;
import com.tmsps.neframework.mvc.annotation.Controller;
import com.tmsps.neframework.mvc.annotation.RequestMapping;
import com.tmsps.neframework.mvc.annotation.ResponseBody;
import com.tmsps.neframework.mvc.plugin.Tip;

@Controller
@RequestMapping("/cp/code")
public class CodeController extends BaseController {

	private static final CodeService codeService = new CodeService();

	@RequestMapping("/list")
	public String list() {
		return "/jsp/code/list.jsp";
	}

	@RequestMapping("/list_data")
	@ResponseBody
	public void list_data(String srh_name) {

		List<Map<String, Object>> list = codeService.selectMyCodeList(srh_name,
				page);

		result.put("list", list);
	}

	@RequestMapping("/add")
	@ResponseBody
	public void add(t_s_code code, int day) {
		code.setEnddate(DateTools.addDay(
				new Timestamp(System.currentTimeMillis()), day));
		code.setStatus_verify("待验证");
		oc.saveObj(code);

		this.setTipMsg(true, "保存成功!", Tip.Type.success);
	}

	@RequestMapping("/edit_form")
	@ResponseBody
	public String edit_form(String kid) {
		t_s_code code = (t_s_code) oc.findObjById(kid, t_s_code.class);

		Map<String, Object> map1 = JsonTools.objToMap("code", code);
		map1.put("code_enddate", DateTools.parse(code.getEnddate()));

		return JsonTools.toJson(map1);
	}

	@RequestMapping("/edit")
	@ResponseBody
	public void edit(t_s_code code, String code_enddate) {
		t_s_code codeDb = (t_s_code) oc.findObjById(code.getKid(),
				t_s_code.class);
		codeDb.setMoney(code.getMoney());
		codeDb.setEnddate(new Timestamp(DateTools.strToDate(code_enddate)
				.getTime()));
		codeDb.setVerify_email(code.getVerify_email());
		codeDb.setVerify_mobile(code.getVerify_mobile());
		codeDb.setVerify_name(code.getVerify_name());
		codeDb.setVerify_qq(code.getVerify_qq());
		oc.updateObj(codeDb);

		this.setTipMsg(true, "保存成功!", Tip.Type.success);
	}

	@RequestMapping("/del")
	@ResponseBody
	public void del(String kid) {
		oc.delObjById(kid, t_s_code.class);

		this.setTipMsg(true, "删除成功!", Tip.Type.success);
	}

	@RequestMapping("/get_code")
	@ResponseBody
	public void get_code() {

		result.put("system_code", GenerateTools.getUUID());
	}

}
